<?php
class Route_Content
{
    /**
     * Build friendly URL
     * @param array $linkArr 0 => <module>, 1 => controller, 2 => action, 3...n param/value
     * @param array $params param => value
     * 
     * @return string friendly URL
     */
    public function build($linkArr, $params = array()) 
    {
    	
    	$result = implode('/', $linkArr);
    	
    	/**
    	 * Link is detail
    	 */
    	if ('detail' == @$linkArr[2]) {
    		/**
	    	 * Structure: pages/<id>/<alias>.html
	    	 */
    		$result = "content/pages/{$linkArr[4]}/";
	    	if (null != @$params['alias']) {
	    		$result .= urlencode(@$params['alias']);
	    	}else {
	    		$content = @Nine_Registry::getContentByGid($linkArr[4]);
	    		if (null != @$content->alias) {
	    			$result .= urlencode(@$content->alias);
	    		}
	    	}
    		 
    	}
    	if ('index' == @$linkArr[2]) {
    		/**
	    	 * Structure: pages/<id>/<alias>.html
	    	 */
    		$result = "catagories/{$linkArr[4]}/";
	    	if (null != @$params['alias']) {
	    		$result .= urlencode(@$params['alias']);
	    	}else {
	    		$content = @Nine_Registry::getContentByGid($linkArr[4]);
	    		if (null != @$content->alias) {
	    			$result .= urlencode(@$content->alias);
	    		}
	    	}
    		 
    	}
    	return $result;
    	
    }
    /**
     * Parse friendly URL
     */
    public function parse()
    {
         $router = Nine_Controller_Front::getInstance()->getRouter();
         $route  = new Zend_Controller_Router_Route_Regex(
                                                 'content/pages/([0-9]+)/(.*).html',
                                                 array(
                                                    'module'     => 'content',
                                                    'controller' => 'index',
                                                    'action'     => 'detail'
                                                 ),
                                                 array(1 =>'id')
                                            );
         $router->addRoute('content', $route); 

         $route1  = new Zend_Controller_Router_Route_Regex(
                                                 'catagories/([0-9]+)/(.*).html',
                                                 array(
                                                    'module'     => 'content',
                                                    'controller' => 'index',
                                                    'action'     => 'index'
                                                 ),
                                                 array(1 =>'cid')
                                            );
         $router->addRoute('content1', $route1); 
         
    }
}